package com.baor.service.impl;

import com.baor.dao.CustomerDao;

import com.baor.domain.CustomerVO;
import com.baor.service.TeamService;
import com.baor.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class TeamServiceImpl implements TeamService {
    @Autowired
    private CustomerDao customerDao;
    @Override
    public R toBalance(Map<String, Object> params) {
        Integer id = (Integer) params.get("id");
        CustomerVO customerVO = this.customerDao.selectById(id);
        Integer usedBanlance = customerVO.getUsedBalance();
        Integer balance = customerVO.getBalance();
        Integer newBalance = balance + usedBanlance;
        customerVO.setBalance(newBalance);
        customerVO.setUsedBalance(0);
        int i = customerDao.updateById(customerVO);
        return i>0 ? R.ok("成功") : R.er();
    }

    @Override
    public R toLockedBalance(Map<String, Object> params) {
        String id = (String) params.get("id");
        CustomerVO customerVO = this.customerDao.selectById(id);
        Integer usedBanlance = customerVO.getUsedBalance();
        Integer lockedBalance = customerVO.getLockedBalance();
        Integer newLockedBalance = usedBanlance + lockedBalance;
        customerVO.setLockedBalance(newLockedBalance);
        customerVO.setUsedBalance(0);
        int i = customerDao.updateById(customerVO);
        return i>0 ? R.ok("成功") : R.er();
    }

    @Override
    public R toWX(Map<String, Object> params) {
        String id = (String) params.get("id");
        Integer money = (Integer) params.get("money");
        CustomerVO customerVO = this.customerDao.selectById(id);
        Integer oldMoney = customerVO.getBalance();
        Integer newMoney = oldMoney - money;
        customerVO.setBalance(newMoney);
        int i = customerDao.updateById(customerVO);

        return i > 0 ? R.ok("转账成功") : R.er();
    }

}
